var fileLocationList = Array();
var ImgsList = Array();
var InfoList = Array();
var SlideTimer;
var TheIndex = -1;

var picerror = "../../resources/query/images/picerror.jpg";

function SlideTo(i){
	
	if(i >= ImgsList.length || i >= InfoList.length)
		return;
	
	arcSize = 0;
	
	var InfoJson = eval('(' + InfoList[i][1] + ')');
	$("#operationName").html(InfoJson.operationName);
	$("#fileTypeName").html(StringCutter(InfoJson.fileTypeName,16));
	$("#fileTypeName").title(InfoJson.fileTypeName);
	$("#pageNumber").html(InfoJson.pageNumber);
	$("#archiveDate").html(InfoJson.archiveDate);
	$("#archiveUser").html(InfoJson.archiveUser);
	$("#verifyDate").html(InfoJson.verifyDate);
	$("#verifyUser").html(InfoJson.verifyUser);
	$("#scanDate").html(InfoJson.scanDate);
	$("#scanUser").html(InfoJson.scanUser);
		
	if(TheIndex >= 0){
		$("#Slides_" + (TheIndex + 1)).className = "";
//		document.getElementById("Slides_" + (TheIndex + 1)).innerHTML = "<a href='javascript:SlideTo("++");'>"+()+"</a>";
	}
	$("#Slides_" + (i + 1)).className = "curpage";
//	document.getElementById("Slides_" + (i+1)).innerHTML = i + 1;
	TheIndex = i;
	
	if(i == 0) {
		$("#imgpre").hide();
	}else {
		$("#imgpre").show();
		document.getElementById("imgpre").onclick = function(){
			SlideTo(TheIndex - 1);
		}
	}
	if(i == (ImgsList.length - 1)) {
		$("#imgnext").hide();
	}else {
		$("#imgnext").show();
		document.getElementById("imgnext").onclick = function(){
			SlideTo(TheIndex + 1);
		}
	}
	
	retrieveThumb(fileLocationList[i]);
	//alert(retrieveThumbResponse);
	
	$("#SlideImg").html("");
	$("<img src=\"" + ImgsList[i][1] + "\" />").appendTo("SlideImg");
	
	if(retrieveThumbResponse == "true")
	{
		$("#SlideImg").html("<img src=\"" + ImgsList[i][1] + "\" onload=\"resizeImage(this, 600)\">");
	}
	else
	{
		$("#SlideImg").html("<img  src=\"" + picerror + "\">");
	}
	
	
	$("#SlideRemark").html(ImgsList[i][0]);
	
	document.getElementById("SlideImg").onclick = function(){
		window.open("viewpicture.jsp?imgsrc=" + ImgsList[i][1]);
	}
	
	//if(TheIndex == (ImgsList.length - 1)){
	//	document.getElementById("SlideImg").onclick = function(){
	//		SlideTo(0);
	//	}
	//}else{
	//	document.getElementById("SlideImg").onclick = function(){
	//		SlideTo(TheIndex + 1);
	//	}
	//}
}

var gaSearcRs;

function createXMLHttpRequest() {
	if(window.ActiveXObject) {
		return new ActiveXObject("Microsoft.XMLHTTP");
	} else if(window.XMLHttpRequest) {
		return new XMLHttpRequest();
	}
}

function getGaSearchRs(indexNo, serial, archiveType, processFileDate, operationType, historyFlag, processType) {
	var url = "../GaQueryRs.do?method=gaSearchRsQuery";
	if(indexNo != "null")
		url += "&indexNo=" + indexNo;
	if(serial != "null")
		url += "&serial=" + serial;
	if(archiveType != "null")
		url += "&archiveType=" + archiveType;	
	if(processFileDate != "null")
		url += "&processFileDate=" + processFileDate;
	if(operationType != "null")
		url += "&operationType=" + operationType;
	if(historyFlag != "null")
		url += "&historyFlag=" + historyFlag;
	if(processType != "null")
		url += "&processType=" + processType;
	if(unarchivedFlag != "null")
		url += "&unarchivedFlag=" + unarchivedFlag;
		
	gaSearcRs = createXMLHttpRequest();
	gaSearcRs.open("POST", url, true);
	gaSearcRs.onreadystatechange = handleGaSearcRs;
	gaSearcRs.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");
	gaSearcRs.send(null);
}

function handleGaSearcRs() {
	if(gaSearcRs.readyState == 4) {
		if(gaSearcRs.status == 200) {
			parseGaSearcRs();
		}
	}
}

function parseGaSearcRs() {
	if(gaSearcRs.responseText == null || gaSearcRs.responseText == "")
		return;
	var gaSearchRsJSON = eval('(' + gaSearcRs.responseText + ')');
	
	var indexNo_Serial = "";
	if(gaSearchRsJSON[0].indexNo != null)
		indexNo_Serial = gaSearchRsJSON[0].indexNo;
	else if(gaSearchRsJSON[0].serial != null)
		indexNo_Serial = gaSearchRsJSON[0].serial;
		
	$("#indexNo_Serial").html(indexNo_Serial);
	
	var k = 0;
	var responseText = "<div  id=controlbar>"
	responseText += "<div><span></span><br/></div>"
	for(var i = 1; i < gaSearchRsJSON.length; i++) {
		var fileTypeName = gaSearchRsJSON[i][0].fileTypeName;
		var fileTypeCode = gaSearchRsJSON[i][0].fileTypeCode;
		
		responseText += "<div><span class=\"headline\">";
		responseText += "<span onclick=\"showFiles(" + fileTypeCode + ")\" title=\"" + fileTypeName + "\" >";
		responseText += StringCutter(fileTypeName,12) + "</span>";
		responseText += "</span></div>";
		
		responseText += "<div id=" + fileTypeCode + ">";
		for(var j = 0; j < gaSearchRsJSON[i].length; j++) {
			fileLocationList[k] = gaSearchRsJSON[i][j].fileLocation;
			var imgsrc = "../../thumb/ga" + gaSearchRsJSON[i][j].fileLocation;
			ImgsList[k] = Array('', imgsrc);
			InfoList[k] = Array('', JSON.stringify(gaSearchRsJSON[i][j]));
			var fileName = fileTypeCode + "-" + gaSearchRsJSON[i][j].pageNumber;
			
			responseText += "<label id=\"Slides_" + k + "\" onclick=\"SlideTo(" + k + ")\"><b>";
			responseText += fileName;
			responseText += "</b></label>";
			k++;
		}
		responseText += "</div>";
	}
	responseText += "</div>";
	responseText += "<p><b></b></p>";
	
	$("#gaCategory").html(responseText);
	SlideTo(0);
}

function showFiles(typeCode){
	$("#" + typeCode).slideToggle("fast");
}

function StringCutter(source, len){
	if(source.length > len){
		source = source.substring(0, len) + "...";
	}
	return source;
}

var retrieveThumbRequest;
var retrieveThumbResponse;
function retrieveThumb(fileLocation)
{
	var url = "/gat/query/RetrieveGAThumbAction.do?fileLocation=" + fileLocation;
	retrieveThumbRequest = createXMLHttpRequest();
	retrieveThumbRequest.open("POST", url, false);
	retrieveThumbRequest.onreadystatechange = handleRetrieveThumbRequest;
	retrieveThumbRequest.send(null);
}

function handleRetrieveThumbRequest()
{
	retrieveThumbResponse = "false";
	if(retrieveThumbRequest.readyState == 4) {
		if(retrieveThumbRequest.status == 200) {
			retrieveThumbResponse = retrieveThumbRequest.responseText;
		}
	}
}

function resizeImage(image, width)
{
	if(!image)
		return;
	
	var w = image.width;
	var h = image.height;
	if(w > width)
	{
		var rationW = w / width;
		w = w / rationW;
		h = h / rationW;
		if(w > 0 && h > 0)
		{
			image.style.width = w + 'px';
			image.style.height = h + 'px';
		}
	}
	
}